package CaseStudyBouncingBall;

import javax.swing.Timer;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Ball extends JPanel {
	private int delay = 10;
	private Timer timer = new Timer(delay, new TimerListener());
	
	private int x = 0; private int y = 0;
	private int radius = 5;
	private int dx = 2;
	private int dy = 2;
	
	public Ball()	{
		timer.start();
	}

	private class TimerListener implements ActionListener	{
		@Override
		public void actionPerformed(ActionEvent arg0) {
			repaint();
		}
	}
	protected void paintComponent(Graphics g)	{
		super.paintComponent(g);
		g.setColor(Color.red);
		if(x < 0 || x > getWidth())
			dx *= -1;
		if(y < 0 || y > getHeight())	
			dy *= -1;
		//adjust ball position
		x += dx;
		y += dy;
		g.fillOval(x - radius, y - radius, radius * 2,  radius *2);
	}
	public void suspend()	{
		timer.stop();
	}
	public void resume()	{
		timer.start();
	}
	public void setDelay(int delay)	{
		this.delay = delay;
		timer.setDelay(delay);
	}
}
